package com.qf.controller;

import com.qf.entity.Cart;
import com.qf.entity.Goods;
import com.qf.entity.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;
import jdk.nashorn.internal.ir.CatchNode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package com.qf.controller
 * @Date 2023/4/14 15:27
 */
@WebServlet(urlPatterns = "/cartservlet")
public class CartServlet extends BaseServlet{
    public String addCart(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");

        if(user==null){
            return "redirect:/login.jsp";
        }
        request.setCharacterEncoding("utf-8");
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if(StringUtils.isEmpty(goodsId)){
            return "redirect:/index.jsp";
        }
        int num=1;
        if(!StringUtils.isEmpty(number)){
            num=Integer.parseInt(number);
            if(num<1){
                num=1;
            }
        }
        CartService cartService=new CartServiceImpl();
        GoodsService goodsService=new GoodsServiceImpl();

        try {
            Cart cart=cartService.query(user.getId(),Integer.parseInt(goodsId));
            Goods goods = goodsService.getGoodsById(goodsId);
            if(cart==null){
                //没有查询到有购物车 增添
                Cart newCart=new Cart(user.getId(),Integer.parseInt(goodsId),num,goods.getPrice().multiply(new BigDecimal(num)));
                cartService.insertCart(newCart);
            }else {
                // 修改
                cart.setNum(cart.getNum()+num);
                cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                cartService.updateCart(cart);
            }
            return "redirect:/cartSuccess.jsp";
        } catch (NumberFormatException e) {
           request.setAttribute("msg","添加购物车失败"+e.getMessage());
           return "/message.jsp";
        }

    }

    public String getCart(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }

        CartService cartService=new CartServiceImpl();
        try {
            List<Cart> cartList=cartService.queryCartByname(user.getId());
            request.getSession().setAttribute("cartList",cartList);
            return "/cart.jsp";

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","查询购物车失败");
            return "/message.jsp";
        }

    }

    public String addCartAjax(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if(StringUtils.isEmpty(goodsId)){
            return "redirect:/index.jsp";
        }
        if(StringUtils.isEmpty(number)){
            return "redirect:/index.jsp";
        }
        try {
            int gid = Integer.parseInt(goodsId);
            int num = Integer.parseInt(number);
            CartService cartService=new CartServiceImpl();
            GoodsService goodsService=new GoodsServiceImpl();
            Goods goods = goodsService.getGoodsById(goodsId);
            Cart cart = cartService.query(user.getId(), gid);
            if(cart!=null){
               if(num==1||num==-1){
                   cart.setNum(cart.getNum()+num);
                   cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                   cartService.updateCart(cart);
               }else if(num==0){
                   cartService.deleteCartByUidGid(user.getId(),gid);
               }
            }
            return null;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg","修改失败");
            return "/message.jsp";
        }



    }

    public String clearCartAjax(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }
        CartService cartService=new CartServiceImpl();
        try {
            cartService.deleteCartByUid(user.getId());

            return null;
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","删除失败");
            return "/message.jsp";
        }

    }

}
